package com.dyz.leetcode;

public class Search33 {
    public static void main(String[] args) {
        int[] nums = new int[]{4,5,6,7,0,1,2};
        int search = new Search33().search(nums, 4);
    }
    public int search(int[] nums, int target) {
        if(nums==null|| nums.length<1){return -1;}
        int left = 0;
        int right = nums.length-1;
        while (left<=right){//一定是等于，不然数组左右边界取不到
            int mid = left + (right - left) / 2;
            if(nums[mid]==target){return mid;}
            //中间值在左边的升序序列中
            if(nums[mid]>=nums[left]){
                //target在左边
                if(nums[left]<=target && nums[mid]>target){
                    right = mid -1;
                }else { //target在右边
                    left = mid +1;
                }

            }
            //中间值位于右边升序序列
            if(nums[mid]<=nums[right]){
                //taregt 位于右边数组
                if(nums[right]>=target && nums[mid]<target){
                    left = mid+1;
                }else {
                    right = mid-1;
                }
            }
        }
        return -1;
    }
}
